package com.mall.order.biz.handler;

import com.mall.commons.tool.exception.BizException;
import com.mall.commons.tool.utils.NumberUtils;
import com.mall.commons.tool.utils.TradeNoUtils;
import com.mall.order.biz.callback.SendEmailCallback;
import com.mall.order.biz.callback.TransCallback;
import com.mall.order.biz.context.CreateOrderContext;
import com.mall.order.biz.context.TransHandlerContext;
import com.mall.order.constant.OrderRetCode;
import com.mall.order.constants.OrderConstants;
import com.mall.order.dal.entitys.Order;
import com.mall.order.dal.entitys.OrderItem;
import com.mall.order.dal.persistence.OrderItemMapper;
import com.mall.order.dal.persistence.OrderMapper;
import com.mall.order.dto.CartProductDto;
import com.mall.order.utils.GlobalIdGeneratorUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 * 初始化订单 生成订单
 */

@Slf4j
@Component
@Transactional
public class InitOrderHandler extends AbstractTransHandler {
    @Autowired
    OrderMapper orderMapper;
    @Autowired
    OrderItemMapper orderItemMapper;

    @Override
    public boolean isAsync() {
        return false;
    }

    @Override
    public boolean handle(TransHandlerContext context) {


        CreateOrderContext createOrderContext = (CreateOrderContext) context;
        Order order = new Order();
        createOrderContext.setOrderId(TradeNoUtils.generateTradeNo());
        order.setOrderId(createOrderContext.getOrderId());
        order.setBuyerMessage(createOrderContext.getBuyerMessage());
        order.setCreateTime(new Date());
        order.setUpdateTime(new Date());
        order.setBuyerComment(0);
        order.setUserId(createOrderContext.getUserId());
        order.setBuyerNick(createOrderContext.getUserName());
        order.setPayment(createOrderContext.getOrderTotal());
        order.setStatus(OrderConstants.ORDER_STATUS_INIT);
        //唯一索引
        //createOrderContext.setUniqueKey(UUID.randomUUID().toString());
        //order.setUniqueKey(createOrderContext.getUniqueKey());
        try {
            int i = orderMapper.insertSelective(order);
        } catch (Exception e) {
            throw new BizException(OrderRetCode.DB_SAVE_EXCEPTION.getCode(), OrderRetCode.DB_SAVE_EXCEPTION.getMessage());
        }


        List<CartProductDto> cartProductDtoList = createOrderContext.getCartProductDtoList();
        createOrderContext.setBuyProductIds(cartProductDtoList.stream().map(CartProductDto::getProductId).collect(Collectors.toList()));
        List<Long> buyProductIds = createOrderContext.getBuyProductIds();
        for (CartProductDto cartProductDto : cartProductDtoList) {
            OrderItem orderItem = new OrderItem();
            orderItem.setId(TradeNoUtils.generateTradeNo());
            orderItem.setOrderId(order.getOrderId());
            orderItem.setItemId(cartProductDto.getProductId());
            orderItem.setNum(cartProductDto.getProductNum().intValue());
            orderItem.setPicPath(cartProductDto.getProductImg());
            orderItem.setPrice(cartProductDto.getSalePrice().doubleValue());
            orderItem.setUpdateTime(new Date());
            //计算订单商品总价
            BigDecimal salePrice = cartProductDto.getSalePrice();
            BigDecimal totalFee = salePrice.multiply(new BigDecimal(cartProductDto.getProductNum()));
            orderItem.setTotalFee(totalFee.doubleValue());
            orderItem.setCreateTime(new Date());
            orderItem.setTitle(cartProductDto.getProductName());
            orderItem.setStatus(1);
            orderItemMapper.insertSelective(orderItem);
        }
        return true;
    }
}
